package de.zainodis.kitchengnome.domainmodel;

import android.os.Parcel;
import android.os.Parcelable;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

import de.zainodis.commons.utils.StringUtils;
import de.zainodis.kitchengnome.domainmodel.persistence.IListItemDao.ListItemDao;

/**
 * An entry of an {@link ItemList}. The combination of an item id and it's
 * owning list id is unique.
 * 
 * @author fzarrai
 * 
 */
@DatabaseTable(tableName = ListItem.TYPE_NAME, daoClass = ListItemDao.class)
public class ListItem implements Parcelable, Comparable<ListItem> {

	public static final String TYPE_NAME = "ListItem";

	public static final String ID_FIELD = "id";
	public static final String TYPE_FIELD = "type";
	public static final String NAME_FIELD = "name";
	public static final String CATEGORY_FIELD = "category";
	public static final String RELATIVE_AMOUNT_FIELD = "relative-amount";
	public static final String NOTES_FIELD = "notes";
	public static final String IS_DONE_FIELD = "is-done";
	public static final String OWNING_LIST_ID_FIELD = "ownerId";

	@DatabaseField(columnName = ID_FIELD, generatedId = true, uniqueCombo = true)
	private long id;

	/** Notes relating to this item */
	@DatabaseField(columnName = OWNING_LIST_ID_FIELD, uniqueCombo = true)
	private long ownerId;

	/** Name of the item */
	@DatabaseField(columnName = NAME_FIELD)
	private String name;

	/** Category to which the item belongs */
	@DatabaseField(columnName = CATEGORY_FIELD)
	private String category;

	/** Relative amount for this item, limited to 100 */
	@DatabaseField(columnName = RELATIVE_AMOUNT_FIELD)
	private int relativeAmount = 0;

	/** Notes relating to this item */
	@DatabaseField(columnName = NOTES_FIELD)
	private String notes = StringUtils.EMPTY;

	/** True if an item has been marked as done; false otherwise */
	@DatabaseField(columnName = IS_DONE_FIELD)
	private boolean isDone = false;

	protected ListItem() {
		// Empty constructor for ormlite
	}

	public ListItem(String name, long owner) {
		this.name = name;
		this.ownerId = owner;
	}

	public ListItem(String name, String category, long owner) {
		this.name = name;
		this.category = category;
		this.ownerId = owner;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public int getAmount() {
		return relativeAmount;
	}

	public void setAmount(int amount) {
		if (amount > 100) {
			this.relativeAmount = 100;
		} else {
			this.relativeAmount = amount;
		}
	}

	public boolean isDone() {
		return isDone;
	}

	/**
	 * If done was true, it's set to false and if it was false, it's set to true.
	 */
	public void toggleDone() {
		isDone = !isDone;
	}

	public long getId() {
		return id;
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long owningListId) {
		this.ownerId = owningListId;
	}

	// Code relevant for serialization (via Parcels)
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
		public ListItem createFromParcel(Parcel in) {
			return new ListItem(in);
		}

		public ListItem[] newArray(int size) {
			return new ListItem[size];
		}
	};

	public ListItem(Parcel parcel) {
		readFromParcel(parcel);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeLong(id);
		parcel.writeLong(ownerId);
		parcel.writeString(name);
		parcel.writeString(category);
		parcel.writeInt(relativeAmount);
		parcel.writeString(notes);
		parcel.writeString(String.valueOf(isDone));
	}

	private void readFromParcel(Parcel parcel) {
		// Read in the same order in which we wrote
		id = parcel.readLong();
		ownerId = parcel.readLong();
		name = parcel.readString();
		category = parcel.readString();
		relativeAmount = parcel.readInt();
		notes = parcel.readString();
		isDone = Boolean.parseBoolean(parcel.readString());
	}

	@Override
	public int compareTo(ListItem another) {
		// Both share the same state
		if (isDone == another.isDone) {
			return 0;
		}
		if (isDone && !another.isDone) {
			return 1;
		} else {
			return -1;
		}

	}
}
